package com.trip.common.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Map;

public class TokenUtil {
    private static final String TOKEN_SECRET = "SDSAFVCVXEEQQWDCFSDF";

    public String createToken(String user_id,String user_name,String role){
        System.out.println(user_id);
        System.out.println(user_name);
        System.out.println(role);
        String sign = JWT.create()
                .withIssuer("auth0")
                .withClaim("user_id",user_id)
                .withClaim("user_name",user_name)
                .withClaim("user_role",role)
                .sign(Algorithm.HMAC512(TOKEN_SECRET));
        return sign;
    }

    public Boolean checkToken(String token){
        try {
            JWTVerifier auth0 = JWT.require(Algorithm.HMAC512(TOKEN_SECRET)).withIssuer("auth0").build();
            DecodedJWT verify = auth0.verify(token);
        }catch (Exception e){
            return false;
        }
        return true;
    }

    public String decodeGetID(String token){
        DecodedJWT decode = JWT.decode(token);
        String user_id = decode.getClaim("user_id").asString();
        return user_id;
    }

    public String decodeGetName(String token){
        DecodedJWT decode = JWT.decode(token);
        String user_name = decode.getClaim("user_name").asString();
        return user_name;
    }

    public String decodeGetRole(String token){
        DecodedJWT decode = JWT.decode(token);
        String role = decode.getClaim("user_role").asString();
        return role;
    }

}
